package cn.darkfire.web.Dao.imp;

import cn.darkfire.web.Dao.UserDao;
import cn.darkfire.web.POJO.LoginUser;
import cn.darkfire.web.Utils.DataUtils;
import cn.darkfire.web.Utils.SQLUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/***
 * 此处实现了用户操作类的所有接口
 * 并需要在此处对这些接口进行功能实现
 */
public class UserDaoImpl implements UserDao {

    @Override
    public List<LoginUser> select() {
        Connection connection= SQLUtils.getInstance().CreateConnection();
        try{
            String sql = "select * from `user`";
            Statement statement = connection.createStatement();
            ResultSet res = statement.executeQuery(sql);
            List<LoginUser> users = new ArrayList<>();
            while (res.next()){
                users.add(new LoginUser(res.getInt("id"),res.getString("name"),res.getString("password")));
            }
            SQLUtils.getInstance().close(res,statement,null);
            return users;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int insert(LoginUser user) {
        Connection connection= SQLUtils.getInstance().CreateConnection();
        try{
            String sql = "insert into `user` values (?,?,?)";
            int id = getUserID();
            if(id==-1){return id;}
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            preparedStatement.setString(2,user.getName());
            preparedStatement.setString(3,user.getPassword());
            if(preparedStatement.execute()){
                return 0;
            }else{
                return -1;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public int delete(String username) {
        Connection connection= SQLUtils.getInstance().CreateConnection();
        try{
            String sql = "delete from `user` where name = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,username);
            if(preparedStatement.execute()){
                return 0;
            }else{
                return -1;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public int update(LoginUser user) {
        Connection connection= SQLUtils.getInstance().CreateConnection();
        try{
            String sql = "update `user` set `password`=? where name =?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(2,user.getName());
            preparedStatement.setString(1, DataUtils.getInstance().stringToMD5(user.getPassword()).toUpperCase());
            int res = preparedStatement.executeUpdate();
            if(res>=1){
                return res;
            }else{
                return -1;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public LoginUser selectByID(String id) {
        Connection connection= SQLUtils.getInstance().CreateConnection();
        String sql = "select * from `user` where id = ?";
        try{
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,Integer.valueOf(id));
            ResultSet res = preparedStatement.executeQuery();
            while (res.next()){
                return new LoginUser(res.getInt("id"),res.getString("name"),res.getString("password"));
            }
        }catch (Exception e){

        }
        return null;
    }

    @Override
    public LoginUser selectByName(LoginUser k) {
        return null;
    }

    @Override
    public LoginUser selectByName(String name) {
        Connection connection= SQLUtils.getInstance().CreateConnection();
        String sql = "select * from `user` where name = ?";
        try{
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            ResultSet res = preparedStatement.executeQuery();
            while (res.next()){
                return new LoginUser(res.getInt("id"),res.getString("name"),res.getString("password"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public List<LoginUser> selectAllByName(String username) {
        List<LoginUser> list = new ArrayList<>();
        Connection connection= SQLUtils.getInstance().CreateConnection();
        String sql = "select * from `user` where name like ?";
        try{
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"%"+username+"%");
            ResultSet res = preparedStatement.executeQuery();
            while (res.next()){
                list.add(new LoginUser(res.getInt("id"),res.getString("name"),res.getString("password")));
            }
        }catch (Exception e){

        }
        return list;
    }

    @Override
    public int getUserID() {
       Connection connection = SQLUtils.getInstance().CreateConnection();
       String sql = "select id from `user` order by id desc limit 0,1 ";
       int id = 0;
       try {
           Statement statement = connection.createStatement();
           ResultSet res = statement.executeQuery(sql);
           while (res.next()){
               id = res.getInt(1);
           }
           System.out.println("max user id->"+id);
           SQLUtils.getInstance().close(res,statement,null);
           return id+1;
       }catch (Exception e){
           e.printStackTrace();
       }

       return -1;
    }
}
